<?php
/**
 * Created by PhpStorm.
 * User: caipeichao
 * Date: 14-3-11
 * Time: PM5:41
 */


namespace Addons\ThreeWords\Controller;

use Home\Controller\AddonsController;

/**
 * Class ThreeWordsController  三字评论
 * @package Addons\Mail\Controller
 * @author:xjw129xjt xjt@ourstu.com
 */
class ThreeWordsController extends AddonsController
{
    protected $wordsModel = null;
    protected $commentModel = null;

    function _initialize()
    {
        parent::_initialize();
        $this->wordsModel = D('Addons://ThreeWords/ThreeWords');
        $this->commentModel = D('Addons://ThreeWords/ThreeWordsComment');
    }

    public function doComment()
    {
        $data['title'] = $title = op_t(I('post.title'));
        $data['color'] = $color = op_t(I('post.color'));
        if(empty($title)){
            $this->error('请填写三字评论');
        }
        $model = op_t(I('request.model'));
        $row_id = intval(I('request.row_id'));
        $word_id = $this->wordsModel->addWords($data);

        $data_comment['model'] = $model;
        $data_comment['word_id'] = $word_id;
        $data_comment['row_id'] = $row_id;
        $data_comment['uid'] = is_login();
        $res = $this->commentModel->addComment($data_comment);

        if ($res) {
            $this->wordsModel->where(array('id'=>$word_id))->setInc('used');
            $this->success('发布成功');
        } else {
            $this->error('发布失败');
        }
    }

    public function delComment()
    {
        $id = op_t(I('post.id'));


        if(!is_login()){
            $this->error('请登录');
        }
        $comment = $this->commentModel->getCommentById($id);
        if(empty($comment)){
            $this->error('评论不存在');
        }

        if($comment['uid'] != is_login() && !is_administrator()){
            $this->error('没有权限操作');
        }
        $res = $this->commentModel->delComment($id);
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function comment(){
        $limit = 10;
        $page = intval(I('request.page'));
        $map['model']=op_t(I('request.model'));
        $map['row_id']=intval(I('request.row_id'));
        $comment = $this->commentModel->getComment($map,$page,$limit);
        $map['status']=1;
        $totalCount = $this->commentModel->where($map)->count();

        $pageCount = ceil($totalCount / $limit);
        $html = getPageHtml('changeThreeWordsPage', $pageCount, '', $page);
        $this->assign('pageHtml',$html);
        $this->assign('comment',$comment);
        $this->display(T('Addons://ThreeWords@ThreeWords/comment'));
    }

}
